var tempDiv = "";
var tempContent = "";
$(document).ready(function () {
    $(".dialog").jqm({modal:true});
    $("#frmCity #submit").remove();
    $(document).ajaxError(function (b, a, c) {
        window.location.reload()
    });
    $(".helpTip").click(function () {
        autoMessage($(this).attr("title"));
        return false
    })
});
function addCommas(b) {
    var a = /,/g;
    b = b.replace(a, "");
    b += "";
    x = b.split(".");
    x1 = x[0];
    x2 = x.length > 1 ? "." + x[1] : "";
    var a = /(\d+)(\d{3})/;
    while (a.test(x1)) {
        x1 = x1.replace(a, "$1,$2")
    }
    return x1 + x2
}
function check_review_path() {
    pathname = window.location.pathname;
    if (pathname.indexOf("checkout") > -1) {
        if (PAGELANG != "") {
            window.location = PAGEROOT + PAGELANG + "/checkout"
        } else {
            window.location = PAGEROOT + "checkout"
        }
    }
}
function numberFormat(c) {
    var b = /,/g;
    c = c.replace(b, "");
    c += "";
    var a = c.split(".");
    var e = a[0];
    var d = a.length > 1 ? "." + a[1] : "";
    b = /(\d+)(\d{3})/;
    while (b.test(e)) {
        e = e.replace(b, "$1,$2")
    }
    return e + d
}
function generateEditTitle(g, e, c, d, f) {
    var b = $("#" + g).attr("title");
    $("#xhgxx_" + g).remove();
    b = b + "<form id='xhgxx_" + g + "' action='" + PAGEROOT + PAGELANG2 + "services/edit2cart.php' method='post'>";
    b = b + "<input type='hidden' name='code' value='" + f + "' />";
    b = b + e;
    b = b + c;
    var a = true;
    if (d != "") {
        b = b + "<br />Ingredients<br />";
        b = b + d
    }
    if (c == d) {
        b = b + message.no_ingredient;
        a = false
    }
    b = b + "</form>";
    if (a) {
        b += '<a href="#" onclick="edit2cart(\'xhgxx_' + g + "'); return false;\">Edit cart</a>"
    }
    $("#" + g).html(b)
}
function GenerateChangeTitle(e, d, c, b) {
    var a = "";
    a = a + "<form id='xhgxx_" + e + "' action='" + PAGEROOT + PAGELANG2 + "services/changegift.php' method='post'>";
    a = a + "<input type='hidden' name='code' value='" + b + "' />";
    a = a + original;
    a = a + options;
    if (d != "") {
        a = a + "<br />Avaliable gift<br />";
        a = a + d;
        a = a + c
    }
    a = a + "</form>";
    a += '<a href="#" onclick="changegift(\'xhgxx_' + e + "'); return false;\">Change gift</a>";
    $("#" + e).html(a)
}
function loadDistricts(c, a) {
    var d = $("#" + c).val();
    if (d > 0) {
        var b = PAGEROOT + "services/getDistricts.php?lang=" + PAGELANG;
        $.post(b, {city:d, defaultValue:a}, function (e) {
            $("#" + c + "_js").html(e)
        })
    }
}
function getDistricts_td(b, a) {
    if (b > 0) {
        $.ajax({url:PAGEROOT + "services/getDistricts_td.php", data:{lang:PAGELANG, city:b}, method:"get", dataType:"json", success:function (d) {
            var f = $("#" + a);
            f.html("");
            if (d.success) {
                for (var c = 0; c < d.result.length; c++) {
                    f.append('<option value="' + d.result[c].id + '">' + d.result[c].name + "</option>")
                }
            } else {
                autoMessage(message.no_district, "successful")
            }
        }})
    }
}
function autoMessage(a) {
    $("#message p").html(a);
    $("#message").jqmShow()
}
function setUpdate() {
    valChange = true
}
function deleteItem(b) {
    var a = $("#quantity_" + b);
    var c = 1;
    if (a.length) {
        c = $("#quantity_" + b).val()
    }
    $.ajax({url:PAGEROOT + "services/ajax.php?action=deleteitem&id=" + b + "&quantity=" + c + "&lang=" + PAGELANG, data:$("#frm_quan").serialize(), type:"post", dataType:"json", beforeSend:function () {
        $("#delete_" + b + " input[type=submit]").attr("disabled", "disabled");
        $("#delete_" + b + " input[type=button]").attr("disabled", "disabled")
    }, complete:function () {
        $("#delete_" + b + " input[type=submit]").removeAttr("disabled");
        $("#delete_" + b + " input[type=button]").removeAttr("disabled");
        $(".jqmOverlay").remove()
    }, success:function (d) {
        if (d.ret == true) {
            $("#frm_quan").html(d.value);
            $("#cart_number").html(d.total_number);
            if (d.error == null) {
                $("#error_cart").html("");
                $("#error_cart").attr("style", "display: none;")
            } else {
                $("#error_cart").html("<p>" + d.error + "</p>");
                $("#error_cart").attr("style", "display: block;")
            }
            if (d.warning == null) {
                $("#warning_cart").html("");
                $("#warning_cart").attr("style", "display: none;")
            } else {
                $("#warning_cart").html("<p>" + d.warning + "</p>");
                $("#warning_cart").attr("style", "display: block;")
            }
            check_review_path()
        }
    }})
}
function edit2cart(a) {
    $.ajax({url:PAGEROOT + "services/ajax.php?action=edit2cart&lang=" + PAGELANG, data:$("#" + a).serialize(), type:"post", dataType:"json", beforeSend:function () {
        $("#" + a + " input[type=submit]").attr("disabled", "disabled");
        $("#" + a + " input[type=button]").attr("disabled", "disabled")
    }, complete:function () {
        $("#" + a + " input[type=submit]").removeAttr("disabled");
        $("#" + a + " input[type=button]").removeAttr("disabled");
        var b = a.replace("xhgxx_cuzz_", "cuzz_");
        $(".jqmOverlay").remove()
    }, success:function (b) {
        if (b.ret == true) {
            $("#frm_quan").html(b.value);
            check_review_path()
        }
    }});
    return false
}
function changeGift() {
    data = "old=" + $("#promotionItemCode").val();
    data += "&new=" + $("#newGift").val();
    data += "&quantity=" + $("#quantityGift").val();
    $.ajax({url:PAGEROOT + "services/ajax.php?action=changegift&lang=" + PAGELANG, data:data, type:"post", dataType:"json", success:function (a) {
        if (a.ret == true) {
            $("#frm_quan").html(a.value);
            autoMessage(a.message, "successful");
            check_review_path()
        }
    }});
    return false
}
;